D:\a\csshw\csshw\src\serde\serialization.rs
Line | Count | Source |
1 | | use windows::Win32::System::Console::{INPUT_RECORD_0, KEY_EVENT_RECORD, KEY_EVENT_RECORD_0}; |
2 | | |
3 | | use crate::serde::SERIALIZED_INPUT_RECORD_0_LENGTH; |
4 | | |
5 | | /// Serialize a [KEY_EVENT_RECORD_0] into a `Vec<u8>` using custom binary format. |
6 | | /// |
7 | | /// Returns the u16 `UnicodeChar` as `Vec<u8>`in little-endian format. |
8 | 1 | pub fn serialize_key_event_record_0(record: &KEY_EVENT_RECORD_0) -> Vec<u8> { |
9 | 1 | return unsafe { record.UnicodeChar }.to_le_bytes().to_vec(); |
10 | 1 | } |
11 | | |
12 | | /// Serialize a [KEY_EVENT_RECORD] into a `Vec<u8>`using custom binary format. |
13 | | /// |
14 | | /// Layout: [1 byte KeyDown][2 bytes RepeatCount][2 bytes VirtualKeyCode] |
15 | | /// [2 bytes VirtualScanCode][2 bytes UnicodeChar][4 bytes ControlKeyState] |
16 | 2 | pub fn serialize_key_event_record(record: &KEY_EVENT_RECORD) -> Vec<u8> { |
17 | 2 | let mut buf = Vec::with_capacity(SERIALIZED_INPUT_RECORD_0_LENGTH); |
18 | | |
19 | | // KeyDown as u8 (1 byte) |
20 | 2 | buf.push(if record.bKeyDown.as_bool() { 1u8 } else { 0u80 }); |
21 | | |
22 | | // RepeatCount as u16 LE (2 bytes) |
23 | 2 | buf.extend_from_slice(&record.wRepeatCount.to_le_bytes()); |
24 | | |
25 | | // VirtualKeyCode as u16 LE (2 bytes) |
26 | 2 | buf.extend_from_slice(&record.wVirtualKeyCode.to_le_bytes()); |
27 | | |
28 | | // VirtualScanCode as u16 LE (2 bytes) |
29 | 2 | buf.extend_from_slice(&record.wVirtualScanCode.to_le_bytes()); |
30 | | |
31 | | // UnicodeChar as u16 LE (2 bytes) |
32 | 2 | buf.extend_from_slice(&unsafe { record.uChar.UnicodeChar }.to_le_bytes()); |
33 | | |
34 | | // ControlKeyState as u32 LE (4 bytes) |
35 | 2 | buf.extend_from_slice(&record.dwControlKeyState.to_le_bytes()); |
36 | | |
37 | 2 | return buf; |
38 | 2 | } |
39 | | |
40 | | /// Serialize an [INPUT_RECORD_0].`KeyEvent` into a `Vec<u8>`using custom binary format. |
41 | | /// |
42 | | /// Panics if the [INPUT_RECORD_0] is not a `KeyEvent`. |
43 | 1 | pub fn serialize_input_record_0(record: &INPUT_RECORD_0) -> Vec<u8> { |
44 | 1 | return serialize_key_event_record(&unsafe { record.KeyEvent }); |
45 | 1 | } |